package de.distmlp.preprocessing.nlp.dictionary;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import de.distmlp.preprocessing.MLDataProcessor;
import de.distmlp.preprocessing.data.MLDataEntry;
import de.distmlp.preprocessing.parser.XingParser;

public class CreateTrainingsData {

	private static final int MIN_TAG_OCCURENCE = 100;
	private static final int MAX_DICTIONARY_SIZE = 100;

	static final String RESOURCES_TAG_CLOUDS = "resources/tagClouds";

	public static void writeTrainingsData(final String path, final List<MLDataEntry> mlData) throws IOException {
		final File f = new File(path);
		if (f.exists()) {
			f.delete();
		}
		f.createNewFile();

		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(f));
			for (final MLDataEntry data : mlData) {
				if (data.toString() != null) {
					writer.append(data.toString() + "\n");
				}
			}
		} catch (final IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	public static void main(final String[] args) {
		final String outputPath = CreateTrainingsData.RESOURCES_TAG_CLOUDS + "_testData_test";
		try {
			final Dictionary d = new Dictionary(CreateTrainingsData.RESOURCES_TAG_CLOUDS, CreateTrainingsData.MIN_TAG_OCCURENCE,
					CreateTrainingsData.MAX_DICTIONARY_SIZE);
			final List<MLDataEntry> mlData = MLDataProcessor.process(CreateTrainingsData.RESOURCES_TAG_CLOUDS, new XingParser(d,
					CreateTrainingsData.MAX_DICTIONARY_SIZE, CreateTrainingsData.MAX_DICTIONARY_SIZE), 10);
			CreateTrainingsData.writeTrainingsData(outputPath, mlData);
			Dictionary.writeDictionary(d, CreateTrainingsData.RESOURCES_TAG_CLOUDS + "_dictionary_test");

		} catch (final IOException e) {
			e.printStackTrace();
		}
	}

}
